#include "mkfifo.h"

int main()
{
    // 建立管道
    if (mkfifo(my_fifo, 0666) < 0)
    {
        perror("mkfifo");
        return 1;
    }

    // 打开文件
    int fd = open(my_fifo, O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        return 2;
    }

    std::cout << "server ready: " << std::endl;

    // 读端逻辑:
    while (true)
    {
        char buffer[128] = {0};
        ssize_t s = read(fd, buffer, sizeof(buffer) - 1);
        if (s > 0)
        {
            buffer[s] = 0; // =='0'
            std::cout << "client write: " << buffer << std::endl;
        }
        else if (s == 0)
        {
            std::cout << "client quit ..." << std::endl;
            break;
        }
        else
        {
            perror("read error!");
            break;
        }
    }

    close(fd);
    return 0;
}